var app = getApp()
var tempFilePaths;

Page({
  /**
   * 页面的初始数据
   */
  data: {
    problem:[
      "请选择问题",
      "问题1",
      "问题2",
      "问题3",
      "问题4"
    ],
    select: 0,
    imageUrl: [],
    _num: 3,
    date: '',
    formData:{
      title: '',
      checked: false,
      description: ''
    }
  },
  bindPickerChange(e){
    this.setData({
      select: e.detail.value
    })
  },
  chooseImg: function(){
    console.log("拍照")
    var that = this;
    wx.chooseImage({
      count: 3, 
      sizeType: ['original', 'compressed'], 
      sourceType: ['album', 'camera'],
      success: function (res) {
        tempFilePaths = res.tempFilePaths
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        that.uploadFile2(tempFilePaths, 0);
        var uploadedImageLists = that.data.imageUrl;
        var totalImages = uploadedImageLists.concat(res.tempFilePaths);
        var aNum = 0;
        if (totalImages.length >= 3) {
          totalImages.length = 3;
          aNum = 1;
        }
        console.log(totalImages)
        that.setData({
          num: 0,
          addNum: aNum,
          imageUrl: totalImages
        })
      }
    })
  },
  uploadFile2: function (file, i) {//递归调用
    var that = this;
    wx.uploadFile({
      url: app.getHeader() +'/SheQu/uploadWxImg', //仅为
      filePath: file[i],
      name: 'img',
      success: function (res) {
        var obj = new Object();
        obj.ind = i + 1;
        var data = res.data;
        console.info(data);
        obj.src = data;
        console.info("--------------*****-------------------");
        console.info(obj);
        if (!((i + 1) == file.length)) {
          total.push(obj);
          that.uploadFile2(file, i + 1);
        } else {
          total.push(obj);
          that.setData({ perImgSrc: total });
        }
      }
    })
  },
  removeImage: function(e){
    var that = this;
    var currentImageLists = that.data.imageUrl; 
    currentImageLists.splice(e.target.dataset.index,1);
    that.setData({
        imageUrl:currentImageLists
    })
    if(currentImageLists.length < 3 && currentImageLists.length >= 1) {
       that.setData({
          addNum: 0
       })
    } else if(currentImageLists.length == 0) {
       that.setData({
            addNum: 1,
            num: 1
       })
    }
  },
  preImage:function(e){
    var that = this;
    var currentImageUrl = that.data.imageUrl[e.target.dataset.index]
    wx.previewImage({
      current: currentImageUrl, // 当前显示图片的http链接
      urls: that.data.imageUrl // 需要预览的图片http链接列表
    })
  },
  chooseVideo:function(e) {
    var that=this;
    var session_key=wx.getStorageSync('session_key');
    wx.chooseVideo({
      maxDuration:10,
      success:function(res1){
        app.startOperating("上传中")
        // 这个就是最终拍摄视频的临时路径了
        var tempFilePath=res1.tempFilePath;
      },
      fail:function(){
        console.error("获取本地视频时出错");
      }
    })

  },
  bindDateChange:function(e){
    this.setData({
      date: e.detail.value
    })
  },
  formSubmit: function(e){
    console.log("提交")
    e.detail.value.imageUrl = tempFilePaths
    e.detail.value.status = "提交成功，等待物业处理"
    e.detail.value.statusId = 0
    e.detail.value.type = this.data.problem[this.data.select]
    var repair = e.detail.value;
    var json = JSON.stringify(repair)    
    wx.setStorageSync('repair', json);
    this.setData({
      formData:{},
      select: 0,
      date: '',
      addNum: 1,
      num: 1
    })
    wx.showToast({
      title: '提交成功',
      duration: 1000,
      icon: 'success'
    })
    // wx.navigateTo({
    //   url: '/pages/repair/repair',
    // })
    // wx.request({
    //   url: app.getHeader() + '/SheQu/insertRepair',
    //   method: 'post',
    //   data: json,
    //   contentType: 'application/json',
    //   success(resp) {
    //     if (res.data == "success") {
    //       wx.showToast({
    //         title: '信息提交成功',
    //         icon: 'success',
    //         duration: 2000
    //       })
    //     } else {
    //       wx.showToast({
    //         title: '信息提交失败',
    //         duration: 1000,
    //         icon: 'none'
    //       })
    //     }
    //   }
    // })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 长按按钮进行录像
   */
  handleLongPress: function (e) {
    // 长按方法触发，调用开始录像方法
    this.startShootVideo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})